# main.py
from src.data_preprocessor import DataPreprocessor
from src.gmm_trainer import GMMTrainer
from src.cache_strategy import GMMCacheManager
from src.utils.config_loader import ConfigLoader
import numpy as np
import joblib

if __name__ == "__main__":
    # 初始化配置
    path_config = ConfigLoader("config/paths.yml").get("paths")
    model_config = ConfigLoader("config/model_params.yml").get("model")

    # 数据预处理
    preprocessor = DataPreprocessor(window_size=100)
    X, df = preprocessor.preprocess(path_config["raw_data"])

    # 模型训练
    trainer = GMMTrainer(model_config)
    model = trainer.train(X)

    # 保存模型
    joblib.dump(model, path_config["model_checkpoint"])
    joblib.dump(preprocessor.scaler, path_config["scaler_checkpoint"])

    # 初始化缓存策略（需连接真实缓存系统）
    cache_manager = GMMCacheManager(
        model=model,
        scaler=preprocessor.scaler,
        threshold=model_config["rerate_threshold"]
    )

    # 模拟后续访问数据（需替换为真实数据）
    y_true = np.random.randint(0, 2, size=len(df))  # 示例数据
    cache_manager.detect_high_value_clusters(X, y_true)

    # 模拟新请求处理
    sample_request = {}  # 替换为真实请求数据
    priority = cache_manager.get_cache_priority(sample_request)
    print(f"Cache priority: {priority}")